import Vue from '@/main'

const Util = {
  prepareBankCards: cards => {
    let len = cards.length
    if (len === 0) {
      return []
    }
    for (let i = 0, l = len; i < l; i++) {
      let bankName = cards[i]['bank'].replace('中国', '')
      cards[i]['bankNameCardNo'] = bankName + '（' + cards[i]['cardNo'] + '）'
    }
    return cards
  },
  /* eslint-disable */
  msg: (msg, callback) => {
    let toast = Vue.$createToast({
      txt: msg,
      type: 'txt',
      time: 3000
    })
    toast.show()

    if (typeof callback === 'function') {
      callback()
    }
  },
  handleResponse: (resp, successMsg, successCallback, errorMsg = '') => {
    successMsg = successMsg ? successMsg : '操作成功'
    let successCode = 1
    if (parseInt(resp.code) === successCode) {
      Util.msg(successMsg, successCallback)
    } else {
      errorMsg = errorMsg ? errorMsg : resp.msg
      Util.msg(errorMsg)
    }
  }
}

export default Util
